package com.xrui.hbaseview.system;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * This class provides an access ot the system clipboard.
 */
public class Clipboard {

    //region Constructor
    private Clipboard() {
    }
    //endregion

    //region Public Methods

    /**
     * Checks if the clipboard has text.
     *
     * @return True if there is any text in the system clipboard or False otherwise.
     */
    public static boolean hasText() {
        Transferable data = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        try {
            String value = (String) data.getTransferData(DataFlavor.stringFlavor);
            return value != null;
        } catch (Exception ignore) {
            return false;
        }
    }

    /**
     * Gets a text from the system clipboard.
     *
     * @return A text if there is any or a null.
     */
    public static String getText() {
        Transferable data = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        try {
            return (String) data.getTransferData(DataFlavor.stringFlavor);
        } catch (Exception ignore) {
            return null;
        }
    }

    /**
     * Sets a provided text to the system clipboard.
     *
     * @param text The text to set.
     */
    public static void setText(String text) {
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
    }
    //endregion
}
